﻿using IronBox.Core.Interfaces;
using IronBox.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace IronBox.Core.Controls
{
    // <summary>
    /// IronBox控件库的基础基类，所有自定义控件继承此类
    /// 封装主题切换、视觉状态管理等共性功能
    /// </summary>
    public abstract class IronBoxControlBase : Control, IIronBoxThemable
    {
        #region 依赖属性：当前主题
        /// <summary>
        /// 当前主题名称（如"ClassicIron"、"DarkIron"）
        /// </summary>
        public string CurrentTheme
        {
            get => (string)GetValue(CurrentThemeProperty);
            set => SetValue(CurrentThemeProperty, value);
        }

        public static readonly DependencyProperty CurrentThemeProperty =
            DependencyProperty.Register(
                nameof(CurrentTheme),
                typeof(string),
                typeof(IronBoxControlBase),
                new PropertyMetadata("ClassicIron", OnCurrentThemeChanged));

        /// <summary>
        /// 主题变更时触发
        /// </summary>
        private static void OnCurrentThemeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is IronBoxControlBase control && e.NewValue is string newTheme)
            {
                control.ApplyTheme(newTheme);
            }
        }
        #endregion

        #region 构造函数
        static IronBoxControlBase()
        {
            // 确保控件使用自定义样式（而非系统默认）
            DefaultStyleKeyProperty.OverrideMetadata(
                typeof(IronBoxControlBase),
                new FrameworkPropertyMetadata(typeof(IronBoxControlBase)));
        }

        protected IronBoxControlBase()
        {
            // 初始化时加载默认主题资源
            LoadDefaultThemeResources();
        }
        #endregion

        #region 主题相关功能（实现IIronBoxThemable接口）
        /// <summary>
        /// 应用指定主题
        /// </summary>
        /// <param name="themeName">主题名称（需在IronBox.Themes中定义）</param>
        public virtual void ApplyTheme(string themeName)
        {
            if (string.IsNullOrEmpty(themeName))
                throw new ArgumentNullException(nameof(themeName));

            // 1. 切换视觉状态（如颜色、阴影等）
            VisualStateManager.GoToState(this, themeName, true);

            // 2. 加载主题对应的资源字典（由资源工具类处理）
            IronBoxResourceLoader.LoadThemeResources(themeName);

            // 3. 触发主题变更事件，供子类扩展
            OnThemeApplied(themeName);
        }

        /// <summary>
        /// 主题应用完成后调用，子类可重写以实现自定义逻辑
        /// </summary>
        protected virtual void OnThemeApplied(string themeName)
        {
            // 空实现，留给子类扩展
        }

        /// <summary>
        /// 加载默认主题资源（控件初始化时调用）
        /// </summary>
        private void LoadDefaultThemeResources()
        {
            IronBoxResourceLoader.LoadThemeResources(CurrentTheme);
        }
        #endregion

        #region 依赖属性快捷注册工具（简化子类注册依赖属性的代码）
        /// <summary>
        /// 快捷注册依赖属性（封装重复的DependencyProperty.Register逻辑）
        /// </summary>
        protected static DependencyProperty RegisterDependencyProperty<T>(
            string propertyName,
            T defaultValue,
            PropertyChangedCallback onChanged = null)
        {
            var metadata = new FrameworkPropertyMetadata(
                defaultValue,
                FrameworkPropertyMetadataOptions.AffectsArrange |
                FrameworkPropertyMetadataOptions.AffectsMeasure |
                FrameworkPropertyMetadataOptions.AffectsRender,
                onChanged);

            return DependencyProperty.Register(
                propertyName,
                typeof(T),
                typeof(IronBoxControlBase),
                metadata);
        }
        #endregion

        #region 视觉状态快捷切换（简化VSM调用）
        /// <summary>
        /// 切换到指定视觉状态
        /// </summary>
        protected bool GoToVisualState(string stateName, bool useTransitions = true)
        {
            return VisualStateManager.GoToState(this, stateName, useTransitions);
        }
        #endregion
    }
}
